﻿/* 关于 DWCore, 您可以参考 http://blog.sina.com.cn/s/blog_604c436d0100o080.html.
 * DWCore 是项目 zsharedcode 的一部分, 您可以参考 http://code.google.com/p/zsharedcode/ 或者 http://blog.sina.com.cn/s/blog_604c436d0100o080.html.
 * Visual Studio 2010 项目模板请访问 http://blog.sina.com.cn/s/blog_604c436d0100o8gn.html.
 * 所有项目源码托管地址: http://panzer.codeplex.com/.
  * Visual Studio 库地址: http://visualstudiogallery.msdn.microsoft.com/b7455318-48ce-4d09-8521-a549ffd08769.
* */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;

using zoyobar.shared.panzer.ui.dwindow;

namespace DWCoreFAPTemplate
{

	public class TemplateWindowCore
		: DataWindowCore<ITemplateWindow, PagerSetting>
	{

		public TemplateWindowCore ( ITemplateWindow customWindow, TableSetting<PagerSetting>[] tableSettings )
			: base(customWindow, tableSettings)
		{ }

		protected override void fillData ( TableSetting<PagerSetting>[] tableSettings )
		{
			base.fillData ( tableSettings );

			// TODO: 在此处修改获取数据库连接字符串。
			using ( OleDbConnection connection = new OleDbConnection ( "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"Template.accdb\";Persist Security Info=True" ) )
			{

				try
				{
					connection.Open ( );

					PagerSetting pagerSetting = tableSettings[0].PagerSetting;

					// TODO: 在此处修改获取窗口上条件的代码。
					string where = string.Empty;

					if ( !string.IsNullOrEmpty ( this.window.GetCustomFullName ( ) ) )
						where += string.Format ( " and fullname like '%{0}%'", this.window.GetCustomFullName ( ) );

					// TODO: 在此处修改获取数据条数的 sql。
					OleDbCommand countCommand = new OleDbCommand ( "select count(*) as C from custom where (1 = 1){0}", connection );

					countCommand.CommandText = string.Format ( countCommand.CommandText, where );

					OleDbDataAdapter countAdapter = new OleDbDataAdapter ( countCommand );
					DataTable countTable = new DataTable ( );
					countAdapter.Fill ( countTable );

					pagerSetting.ItemCount = Convert.ToInt32 ( countTable.Rows[0]["C"] );

					// TODO: 在此处修改获取分页数据的 sql，形式为：select <字段> from (select <字段>, (select count(*) from custom where <排序字段> <= X.<排序字段> <条件>) as RN from <表> as X where (1 = 1) <条件> order by <排序字段> asc) as Y where RN >= {1} and RN <= {2}
					OleDbCommand fillCommand = new OleDbCommand ( "select ID, FullName, Age, MobilePhone, Address, RegisterDate from (select ID, FullName, Age, MobilePhone, Address, RegisterDate, (select count(*) from custom where id <= X.id{0}) as RN from custom as X where (1 = 1){0}  order by id asc) as Y where RN >= {1} and RN <= {2}", connection );
					fillCommand.CommandText = string.Format ( fillCommand.CommandText, where, pagerSetting.BeginRowNO, pagerSetting.EndRowNO );

					OleDbDataAdapter fillAdapter = new OleDbDataAdapter ( fillCommand );
					tableSettings[0].Table.Clear ( );
					fillAdapter.Fill ( tableSettings[0].Table );
				}
				catch
				{ MessageBox.Show ( "获取数据失败!" ); }
				finally
				{ connection.Close ( ); }

			}

		}

	}

}
